package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatChannelPackage;
import com.happy.cat.project.system.service.ICatChannelPackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 渠道包Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/channelPackage" )
@Api("渠道包 API")
public class CatChannelPackageController extends BaseController {

    private final ICatChannelPackageService iCatChannelPackageService;

    @GetMapping("/load" )
    public AjaxResult load() {
        List<CatChannelPackage> list = iCatChannelPackageService.list();
        return AjaxResult.success(list);
    }

    /**
     * 查询渠道包列表
     */
    @ApiOperation(value = "查询渠道包列表",notes = "查询渠道包列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:channelPackage:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatChannelPackage catChannelPackage)
    {
        startPage();
        LambdaQueryWrapper<CatChannelPackage> lqw = new LambdaQueryWrapper<CatChannelPackage>();
        if (StringUtils.isNotBlank(catChannelPackage.getDownLink())){
            lqw.eq(CatChannelPackage::getDownLink ,catChannelPackage.getDownLink());
        }
        if (StringUtils.isNotBlank(catChannelPackage.getRemarks())){
            lqw.eq(CatChannelPackage::getRemarks ,catChannelPackage.getRemarks());
        }
        if (catChannelPackage.getCreateDate() != null){
            lqw.eq(CatChannelPackage::getCreateDate ,catChannelPackage.getCreateDate());
        }
        if (catChannelPackage.getStatus() != null){
            lqw.eq(CatChannelPackage::getStatus ,catChannelPackage.getStatus());
        }
        List<CatChannelPackage> list = iCatChannelPackageService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出渠道包列表
     */
    @ApiOperation(value = "导出渠道包列表",notes = "导出渠道包列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:channelPackage:export')" )
    @Log(title = "渠道包" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatChannelPackage catChannelPackage) {
        LambdaQueryWrapper<CatChannelPackage> lqw = new LambdaQueryWrapper<CatChannelPackage>(catChannelPackage);
        List<CatChannelPackage> list = iCatChannelPackageService.list(lqw);
        ExcelUtil<CatChannelPackage> util = new ExcelUtil<CatChannelPackage>(CatChannelPackage. class);
        return util.exportExcel(list, "channelPackage" );
    }

    /**
     * 获取渠道包详细信息
     */
    @ApiOperation(value = "获取渠道包详细信息",notes = "获取渠道包详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:channelPackage:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatChannelPackageService.getById(id));
    }

    /**
     * 新增渠道包
     */
    @ApiOperation(value = "新增渠道包",notes = "新增渠道包",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:channelPackage:add')" )
    @Log(title = "渠道包" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatChannelPackage catChannelPackage) {
        catChannelPackage.setCreateDate(new Date());
        catChannelPackage.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatChannelPackageService.save(catChannelPackage) ? 1 : 0);
    }

    /**
     * 修改渠道包
     */
    @ApiOperation(value = "修改渠道包",notes = "修改渠道包",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:channelPackage:edit')" )
    @Log(title = "渠道包" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatChannelPackage catChannelPackage) {
        catChannelPackage.setCreateDate(new Date());
        catChannelPackage.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatChannelPackageService.updateById(catChannelPackage) ? 1 : 0);
    }

    /**
     * 删除渠道包
     */
    @ApiOperation(value = "删除渠道包",notes = "删除渠道包",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:channelPackage:remove')" )
    @Log(title = "渠道包" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatChannelPackageService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
